<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../js/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <script>
        layui.use(['table','jquery'], function(){
            var $ = layui.$;
            $(document).on('click', '.', function() {
                layer.msg('响应点击事件');
            });
            function myFunction(){
                alert()
            }
        });
    </script>
</head>

<body>

<div class="demoTable">
    商品名：
    <div class="layui-inline">
        <input class="layui-input" name="productId" id="demoReload" autocomplete="off">
    </div>
    <button class="layui-btn" data-type="reload">搜索</button>
</div>

<table class="layui-hide" id="LAY_table_user" lay-filter="user"></table>

<div>

    <button onclick="myFunction()">点我</button>
</div>
<script src="../../../js/layui/layui.js"></script>
<script src="../../../js/myJQ.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['table','jquery'], function(){
        var table = layui.table;
        var $ = layui.$;
        //方法级渲染
        table.render({
            elem: '#LAY_table_user'
            ,url:'/test/getProductList_v1'
            ,method:"POST"
            ,cols: [[
                {field:'productId', width:80, title: '商品ID', sort: true}
                ,{field:'productName', width:100, title: '商品名'}
                ,{field:'img', width:100, title: '图片'}
                ,{field:'sort', width:100, title: '排序'}
                ,{field:'description', width:100, title: '描述'}
            ]]
            ,id: 'testReload'
            ,page: true
            ,height: 310
        });

        /**
         * 这个方法实现把数据
         */



        function toRequestBody_v2(){
            var param = {};
            param.productId=1;
            param.productName="hahah";
            param.img="test.jsp";
            $.ajax({
                url:getRootPath()+"/test/getProductList_v3",
                data:JSON.stringify(param),
                type:"post",
                contentType:"application/json; utf-8",//
                success:function (result) {
                    console.log(result);
                }
            })
        }


        /**
         * 后台用@RequestBody接收
         */
        function toRequestBody(){
            var param = {};
            param.productId=1;
            param.productName="hahah";
            param.img="test.jsp";
            $.ajax({
                url:getRootPath()+"/test/getProductList_v3",
                //data:param,
                data:JSON.stringify(param),
                contentType:"application/json; utf-8",//必须有
                type:"post", //目前测试之用post才行，get不行
                success:function (result) {
                    console.log(result);
                }
            })
        }

        var $ = layui.$, active = {
            reload: function(){
                debugger
                var demoReload = $('#demoReload');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        productName: demoReload.val().trim()
                    }
                }, '');
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            debugger
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

</body>
</html>